﻿Public Class FormInvoice
    Private Sub FormInvoice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       

        UI.FormatDataGrid(Me.dgv)

        Me.cboStore.DisplayMember = "StoreName"
        Me.cboStore.ValueMember = "StoreId"
        Me.cboStore.DataSource = Cmd.ExecuteDataTable("SELECT StoreId,StoreName FROM Store WHERE Active=1 UNION SELECT 0,N'គ្រប់ឃ្លាំង'")

        LoadData()
    End Sub

    Public Sub LoadData()
        Dim sql = <sql> 
SELECT p.*,v.CustomerName,s.StoreName 
FROM Invoice p 
INNER JOIN Customer v ON v.CustomerId=p.CustomerId 
INNER JOIN Store s ON s.StoreId=p.StoreId 
WHERE p.Active=1 
    AND InvoiceDate BETWEEN @D1 AND @D2 
    AND (@CustomerId=0 OR p.CustomerId=@CustomerId ) 
    AND (@StoreId=0 OR p.StoreId=@StoreId) 
    AND UPPER(InvoiceNo) LIKE UPPER(@S);
                 </sql>.Value

        Cmd.Parameters("@D1") = Me.DateTimePicker1.Value.Date
        Cmd.Parameters("@D2") = Me.DateTimePicker2.Value.Date.AddDays(1).AddSeconds(-1)
        Cmd.Parameters("@StoreId") = Me.cboStore.SelectedValue
        Cmd.Parameters("@CustomerId") = 0
        Cmd.Parameters("@S") = "%" + Me.txtSearch.Text + "%"
        Me.dgv.DataSource = Cmd.ExecuteDataTable(sql)

    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        LoadData()
    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormInvoiceEditor()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Cmd.Begin()
        Try
            Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
            Dim id = row.Cells("InvoiceId").Value

            Cmd.Parameters("@InvoiceId") = id
            Dim storeId = Cmd.ExecuteScalar("SELECT StoreId FROM Invoice WHERE InvoiceId=@InvoiceId")

            Cmd.Parameters("@InvoiceId") = id
            Cmd.ExecuteNonQuery("UPDATE Invoice SET Active=0 WHERE InvoiceId=@InvoiceId")

            'load details to remove
            Cmd.Parameters("@InvoiceId") = id
            Dim dt As DataTable = Cmd.ExecuteDataTable("SELECT * FROM InvoiceDetail WHERE Active=1 AND  InvoiceId = @InvoiceId")

            For Each r As DataRow In dt.Rows
                'remove stock
                Stock.Update(storeId, r("ProductId"), r("Qty"))
                'remove detail
                Cmd.Parameters("@InvoiceDetailId") = r("InvoiceDetailId")
                Cmd.ExecuteNonQuery("UPDATE InvoiceDetail SET Active=0 WHERE InvoiceDetailId=@InvoiceDetailId")
            Next

            Cmd.Commit()
            LoadData()

        Catch ex As Exception
            Cmd.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub cboStore_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStore.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.dgv.SelectedRows(0)
        Dim frm = New FormInvoiceEditor()
        frm.cboStore.Enabled = False
        frm.cboCustomer.Enabled = False
        frm.txtInvoiceId.Text = row.Cells("InvoiceId").Value
        frm.txtInvoiceNo.Text = row.Cells("InvoiceNo").Value
        frm.cboStore.SelectedValue = row.Cells("StoreId").Value
        frm.cboCustomer.SelectedValue = row.Cells("CustomerId").Value
        frm.txtSubTotal.Text = row.Cells("SubTotal").Value
        frm.txtDiscount.Text = row.Cells("Discount").Value
        frm.txtTotal.Text = row.Cells("Total").Value
        frm.LoadDetailData(row.Cells("InvoiceId").Value)
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub
End Class